var mongoose = require('mongoose')
mongoose.Promise = global.Promise;

var db = mongoose.createConnection('localhost', 'example1_test5')
db.on('error', console.error.bind(console, '连接错误:'));

db.once('open', function () {
    var PersonSchema = new mongoose.Schema({
        name: String,
        type: String
    })

    // 查询和当前实例type相同的数据
    PersonSchema.methods.findSimilarTypes = function (cb) {
        return this.model('Person').find({
            type: this.type
        }, cb)
    }

    var PersonModel = db.model('Person', PersonSchema);

    var personEntity = new PersonModel({
        name: 'a',
        type: 'b'
    })

    personEntity.save()
        .then(function(){
            personEntity.findSimilarTypes(function(err, persons){
                console.log(persons)
            })
        })

})

    // personEntity.save()
    //     .then(function(){
    //         // personEntity1.findSimilarTypes(err, persons){
    //         //     console.log(persons)
    //         // }
    //     })
